﻿/*
=======================================================
    MagnetHead【磁头】
=======================================================
*/

namespace Services
{
    public interface IMagnetHeadLogic : ILogic<MagnetHeadModel, MagnetHeadFilterModel>
    {
        Task<PageModel<MagnetHeadModel>> GetStatusMonitorPage(MagnetHeadFilterModel filterModel, int pageIndex, int pageSize, string orderBy);
    }

    public class MagnetHeadLogic : BaseLogic<MagnetHeadModel, MagnetHeadFilterModel, MagnetHead, IMagnetHeadRepository, IMagnetHeadMapping, IMagnetHeadService>, IMagnetHeadLogic
    {
        #region Ioc
        private readonly IMHCheckRepository _IMHCheckRepository;
        private readonly IMHCheckMapping _IMHCheckMapping;

        public MagnetHeadLogic(ICurrentUser currentUser, IMagnetHeadRepository repository, IMagnetHeadMapping mapping, IMagnetHeadService service,
            IMHCheckRepository iMHCheckRepository,
            IMHCheckMapping iMHCheckMapping)
            : base(currentUser, repository, mapping, service)
        {
            _IMHCheckRepository = iMHCheckRepository;
            _IMHCheckMapping = iMHCheckMapping;
        }
        #endregion

        #region 状态监测列表
        public async Task<PageModel<MagnetHeadModel>> GetStatusMonitorPage(MagnetHeadFilterModel filterModel, int pageIndex, int pageSize, string orderBy)
        {
            var pageData = GetPage(filterModel, pageIndex, pageSize, orderBy);
            var mhIds = pageData.Items.Select(x => x.MHID).ToList();
            var lastChecks = await _IMHCheckRepository.GetLatestMHChecks(mhIds);

            foreach (var item in pageData.Items)
            {
                if (lastChecks.TryGetValue(item.MHID, out var lastCheck))
                {
                    item.LastMHCheckModel = _IMHCheckMapping.Entity2Model(lastCheck); ;
                }
            }

            return pageData;
        }
        #endregion
    }
}
